//--------------------------------------------------------------------------------
// This file is part of the Windows Workflow Foundation Sample Code
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation.  See these other
// materials for detailed information regarding Microsoft code samples.
// 
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//--------------------------------------------------------------------------------

using System;
using System.Web;
using System.Security.Principal;
using System.Threading;

namespace Microsoft.Samples.Workflow.UI.Asp
{
    public class AspNetUserInput : UserInput
    {
        ActionType actionType;
        IPrincipal principal;

        public AspNetUserInput()
            : base()
        {
            this.principal = Thread.CurrentPrincipal;
        }

        public AspNetUserInput(string action, object data, IPrincipal principal)
            : base(action, data)
        {
            this.principal = principal;
        }

        public IPrincipal Principal
        {
            get { return this.principal; }
            set { this.principal = value; }
        }

        public static void GoForward(object data, IPrincipal principal)
        {
            Submit(ActionType.Forward, null, data, principal);
        }

        public static void GoForward(string action, object data, IPrincipal principal)
        {
            Submit(ActionType.Forward, action, data, principal);
        }

        public static void GoBack(IPrincipal principal)
        {
            Submit(ActionType.Back, null, null, principal);
        }

        public static void Cancel(IPrincipal principal)
        {
            Submit(ActionType.Cancel, null, null, principal);
        }
        public static Exception GetCurrentException()
        {
            HttpContext currentContext = WFWebHostingModule.GetHttpContext();
            Exception exp = currentContext.Session[SessionVariableLookupKeys.Exception] as Exception;
            currentContext.Session.Remove(SessionVariableLookupKeys.Exception);
            return exp;

        }
        private static void Submit(ActionType actionType, string action, object data, IPrincipal principal)
        {
            AspNetUserInput input = new AspNetUserInput(action, data, principal);
            input.actionType = actionType;
            HttpContext currentContext = WFWebHostingModule.GetHttpContext();
            Guid workflowId = Guid.Empty;
            if (currentContext.Session[SessionVariableLookupKeys.InstanceId] != null)
            {
                workflowId = (Guid)currentContext.Session[SessionVariableLookupKeys.InstanceId];
                // Need to use Session if using handler.
                currentContext.Items[SessionVariableLookupKeys.UserInput] = input;
            }
        }

        internal ActionType ActionType
        {
            get { return this.actionType; }
        }
    }

    public enum ActionType
    {
        Forward = 0,
        Back = 1,
        Cancel = 2
    }
}
